package com.example.dyy.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dyy.entity.Movie;
import com.example.dyy.util.DBUtil;

@WebServlet("/movie/edit")
public class MovieEditServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String id = request.getParameter("id");
        
        String sql = "SELECT * FROM movie WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, Integer.parseInt(id));
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                Movie movie = new Movie();
                movie.setId(rs.getInt("id"));
                movie.setName(rs.getString("name"));
                movie.setDuration(rs.getInt("duration"));
                movie.setType(rs.getString("type"));
                movie.setDescription(rs.getString("description"));
                movie.setPrice(rs.getDouble("price"));
                
                request.setAttribute("movie", movie);
                request.getRequestDispatcher("/movie/edit.jsp").forward(request, response);
            } else {
                response.sendRedirect("list");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException("获取电影信息失败", e);
        }
    }
} 